﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;
using MReviewerClient.MReviewerSvc;
using MReviewerClient.Providers;
using MReviewerClient.Models;

namespace MReviewerClient.Views
{
    public partial class RecordEstablishmentView : PhoneApplicationPage
    {
        #region Fields

        /// <summary>
        /// The list of establishment
        /// </summary>
        public ObservableCollection<EstablishmentTypeDTO> EstablishmentTypes { get; set; }

        /// <summary>
        /// The list of FoodType
        /// </summary>
        public ObservableCollection<FoodTypeDTO> FoodTypes { get; set; }

        #endregion

        #region Constructor

        /// <summary>
        /// The default constructor
        /// </summary>
        public RecordEstablishmentView()
        {
            InitializeComponent();
            // Load data
            this.LoadAllEstablishmentTypes();
            this.LoadAllFoodTypes();
            this.DataContext = this;
        }

        #endregion

        #region Methods & Handlers

        /// <summary>
        /// Load list all of the establishment Type
        /// </summary>
        public void LoadAllEstablishmentTypes()
        {
            this.EstablishmentTypes = EstablishmentTypeModel.Instance.EstablishmentTypes;
        }

        /// <summary>
        /// Load list all of the food Type
        /// </summary>
        public void LoadAllFoodTypes()
        {
            this.FoodTypes = FoodTypeModel.Instance.FoodTypes;
        }

        /// <summary>
        /// Input validation
        /// </summary>
        /// <param name="errTxt"></param>
        /// <returns></returns>
        private bool ValidateUserInput(ref string errTxt)
        {
            if (txtEstablishmentID.Text.Equals(string.Empty))
            {
                errTxt = "Please input the Establishment ID";
                return false;
            }
            if (txtName.Text.Equals(string.Empty))
            {
                errTxt = "Please input the Establishment Name";
                return false;
            }
            if (lstEstblismentType.SelectedIndex == -1)
            {
                errTxt = "Please input the Establishment Type";
                return false;
            }
            return true;
        }

        /// <summary>
        /// The handler for insert new Establishment
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ApplicationBarIconButton_Click(object sender, EventArgs e)
        {
            var errTxt = string.Empty;
            if (this.ValidateUserInput(ref errTxt) == false)
            {
                MessageBox.Show(errTxt, "Error", MessageBoxButton.OK);
                return;
            }

            // This action will be executed when the operation for selecting from service is completely
            Action actionInformResult = () =>
            {
                if (EstablishmentModel.Instance.InsertResult)
                    MessageBox.Show("The new establishment has been added", "Information", MessageBoxButton.OK);
                else
                    MessageBox.Show("Cannot insert the new establishment!", "Error", MessageBoxButton.OK);
            };

            // Store information of new establishment which will be inserted
            var establishment = new EstablishmentDTO
            {
                ID = txtEstablishmentID.Text,
                IDEstablishmentType = (lstEstblismentType.SelectedItem as EstablishmentTypeDTO).ID,
                IDFoodType = (lstFoodType.SelectedItem as FoodTypeDTO).ID,
                Name = txtName.Text,
                Location = txtLocation.Text
            };
            // Start to insert new establishment
            EstablishmentModel.Instance.Insert(establishment, actionInformResult);
        }

        #endregion
    }
}